
#ifndef LESSON2_SCAN_TO_ContnavLogger_H
#define LESSON2_SCAN_TO_ContnavLogger_H
//#pragma once

#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <cstdlib>
#include <string.h>
#include <string>
using std::string;

namespace contnav {
namespace     log {
/**
 *
 */
#define __DEBUG__
#define __INFO__
#define __WARN__
#define __ERROR__
#define __FATAL__

#ifdef __DEBUG__
#define contnav_debug(format, ...) \
    ContnavLogger::instance()->log_debug(__FILE__, __LINE__, format, ##__VA_ARGS__)
#else
#define debug(format, ...)
#endif

#ifdef __INFO__
#define contnav_info(format, ...) \
    ContnavLogger::instance()->log_info(__FILE__, __LINE__, format, ##__VA_ARGS__)
#else
#define info(format, ...)
#endif

#ifdef __WARN__
#define contnav_warn(format, ...) \
    ContnavLogger::instance()->log_warn(__FILE__, __LINE__, format, ##__VA_ARGS__)
#else
#define warn(format, ...)
#endif

#ifdef __ERROR__
#define contnav_error(format, ...) \
    ContnavLogger::instance()->log_error(__FILE__, __LINE__, format, ##__VA_ARGS__)
#else
#define error(format, ...)
#endif

#ifdef __FATAL__
#define contnav_fatal(format, ...) \
    ContnavLogger::instance()->log_fatal(__FILE__, __LINE__, format, ##__VA_ARGS__)
#else
#define fatal(format, ...)
#endif


class ContnavLogger
{
public:
    enum Level
    {
        DEBUG = 0,
        INFO,
        WARN,
        ERROR,
        FATAL,
        LEVEL_COUNT
    };

    ContnavLogger();
    ~ContnavLogger();

    static ContnavLogger* instance();
    void open(const string &logfile);
    void close();

    void log_debug(const char* file, int line, const char* format, ...);
    void log_info(const char* file, int line, const char* format, ...);
    void log_warn(const char* file, int line, const char* format, ...);
    void log_error(const char* file, int line, const char* format, ...);
    void log_fatal(const char* file, int line, const char* format, ...);

protected:
    void log(Level level, const char* file, int line, const char* format, va_list arg_ptr);

protected:
    FILE *m_fp;
    static const char* s_level[LEVEL_COUNT];
    static ContnavLogger *m_instance;
};

}}
#endif // LESSON2_SCAN_TO_ContnavLogger_H